import lib.modula.TxtResolver;

var txtResolver;

function getInstance(target:MovieClip)
{
	txtResolver=new TxtResolver(target);
	if (Packages.task.Composition.content!=null)
	{
		// txtResolver.content=Packages.task.Composition.content;
		display(Packages.task.Composition.content);
	}
	if (Packages.task.Composition.readerSettings.block!=null)
	{
		// trace("getInstance.checkSetBlock...");
		setBlock(Packages.task.Composition.readerSettings.block);
	}
	if (Packages.task.Composition.readerSettings.fmt!=null)
	{
		// trace("getInstance.checkApplyFormat...");
		applyFormat(Packages.task.Composition.readerSettings.fmt);
	}
}

function display(content:String)
{
	txtResolver.content=content;
	txtResolver.scanBlocks(100);		// 100 as default
	txtResolver.switchBlock(null, 0);
	txtResolver.initFillingBlock(txtResolver.path._width-1, 20, 1, -20);
}

function applyFormat(fmtObj:Object)
{
	txtResolver.changeDisplayFormat(fmtObj);
}

function setBlock(blockParam, startFilling:Boolean)
{
	txtResolver.speed=blockParam.speed;
	txtResolver.scanBlocks(blockParam.size, 500);
	if (!startFilling) return;
	txtResolver.stopFillingBlock();
	txtResolver.switchBlock(null, txtResolver.accessCurrentBlockNum());
	txtResolver.startFillingBlock();
}

function switchBlock(offset:Number, recordFlag:Boolean, preOffsetFlag:Boolean)
{
	// var lastBlockLength=txtResolver.accessCurrentBlockLength();
	if (recordFlag)
	{
		// Packages.task.Processor.addTimeStamp(lastBlockLength, txtResolver.accessCurrentBlockNum());
		Packages.task.Processor.addTimeStamp(preOffsetFlag, txtResolver.accessCurrentBlockNum());
	}

	txtResolver.stopFillingBlock();
	if (offset==1 || offset==-1)
	{
		txtResolver.switchBlock(offset);
		if (recordFlag) txtResolver.startFillingBlock();
	}
	else if (offset==-2)
	{
		txtResolver.switchBlock(null, 0);
		if (recordFlag) txtResolver.startFillingBlock();
	}

	displayBlockProgress();
}

function displayBlockProgress()
{
	var blkProgress=mlf.gatekeeper.native.Caller.scanElement("RecordingSpace", "blockProgress");
	blkProgress.text=Number(txtResolver.accessCurrentBlockNum()+1)+" / "+txtResolver.accessTotalBlockNum();
}

function removeInstance()
{
	// Packages.task.Composition.content=txtResolver.content;
	// Packages.task.Composition.readerSettings=txtResolver.status;
	// Packages.task.Composition.readerSettings.displayScroll=1;
	// Packages.task.Composition.readerSettings.blockNum=0;
	txtResolver.stopFillingBlock();
	delete txtResolver;
}

function accessBlockInfo():Object
{
	return {current:txtResolver.accessCurrentBlockNum(), total:txtResolver.accessTotalBlockNum()};
}